package com.simpleframe.views.components;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.dom4j.util.UserDataAttribute;

import com.simpleframe.core.io.ClassPathResource;
import com.simpleframe.core.io.Resource;
import com.simpleframe.exception.SimpleframeException;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class Component {
	/** 存放标签属性值 */
	protected Map<String, Object> parameters;
	private String TAG_DEFAULT_TEMPLATE_DIR = "templates/def";
	private String TAG_USER_DEFAULT_TEMPLATE_DIR = "templates/usr";
	private boolean IS_USE_USER = false;
	
	
	public boolean start(Writer writer){
		return true;
	}
	
	public boolean end(Writer writer, String tagBody){
		try {
			writer.write(tagBody);
		} catch (IOException e) {
			throw new SimpleframeException("输出标签时发生IO异常：" + e.getMessage(), e);
		}
		return false;
	}
	
	protected  String getComponentName(){
		Class<?> c = getClass();
        String name = c.getName();
        int dot = name.lastIndexOf('.');

        return name.substring(dot + 1).toLowerCase();
	}
	
	protected String getTemplateDir(){
		Resource resource = null;
		if(IS_USE_USER){
			resource = new ClassPathResource(TAG_USER_DEFAULT_TEMPLATE_DIR);
		}else {
			resource = new ClassPathResource(TAG_DEFAULT_TEMPLATE_DIR);
		}
		try {
			return resource.getFile().getPath();
		} catch (IOException e) {
			throw new SimpleframeException("标签路径不存在："+e.getMessage(),e);
		}
	}
	
	public static void main(String[] args) {
		Component component = new Component();
	}
	
	protected String getTemplate(){
		String user_def = getComponentName()+".ftl";
		Resource resource = new ClassPathResource(TAG_USER_DEFAULT_TEMPLATE_DIR+"/"+user_def);
		if(resource.exists()){
			IS_USE_USER = true;
			return user_def;
		}
		return getComponentName()+".ftl";
	}
	
    public Map<String,Object> getParameters() {
        return parameters;
    }

    public void addAllParameters(Map<String,Object> params) {
        parameters.putAll(params);
    }

    public void addParameter(String key, Object value) {
    	
    	if(parameters == null){
    		parameters = new HashMap<String, Object>();
    	}
    	
        if (key != null) {
            parameters.put(key, value);
        }
    }
    
    public boolean usesBody(){
    	return false;
    }
    
    public void mergeTemplate(Writer writer, String template){
		Configuration configurable = new Configuration();
		try {
			configurable.setDirectoryForTemplateLoading(new File(getTemplateDir()));
			Template t = configurable.getTemplate(template, "UTF-8");   
			t.process(parameters, writer);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
    /**
     * 生成一个以时间命名，不重复的ID
     * @return
     */
    public String getAutoID(){
    	return "auto_id_"+System.currentTimeMillis();
    }
	
}
